package com.ross.web.inquiry;

import java.awt.Color;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.SoldProductsDAO;
import com.ross.web.domain.SoldProducts;
import com.ross.web.util.SessionLogout;

public class SoldProductsAction extends ActionSupport {

	private int topanswer;
	private List<SoldProducts> soldProductsList;
	private int isListEmpty;
	private InputStream fileStream;

	private SessionLogout sessionLogout = new SessionLogout();
	private HttpServletRequest request = ServletActionContext.getRequest();

	SoldProductsDAO soldproductsDAO = new SoldProductsDAO();

	public String goToSoldProductsAction() {

		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}

		setIsListEmpty(1);
		setTopanswer(10);
		return SUCCESS;
	}

	public String doGetSoldProductsAction() {
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}

		soldProductsList = soldproductsDAO.getAllSoldProducts(getTopanswer());
		if (soldProductsList.isEmpty()) {
			setIsListEmpty(1);
		} else {
			request.getSession().setAttribute("soldProductsList", soldProductsList);
			setIsListEmpty(0);
		}
		
		setTopanswer(getTopanswer());
		return SUCCESS;
	}

	public String viewAsChartAction() {

		try {
			
			String templatePath = request.getSession().getServletContext().getRealPath("/") + "reporttemplates\\chart.jrxml";
			String destinationPath = request.getSession().getServletContext().getRealPath("/") + "reporttemplates";
			soldProductsList = (List<SoldProducts>) request.getSession().getAttribute("soldProductsList");
			
			JasperReport jasperReport;
			JasperPrint jasperPrint;
			jasperReport = JasperCompileManager.compileReport(templatePath);
			JRBeanCollectionDataSource jbc = new JRBeanCollectionDataSource(soldProductsList);
			jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), new JRBeanCollectionDataSource(soldProductsList,false));
			JasperExportManager.exportReportToPdfFile(jasperPrint, destinationPath+"\\chart.pdf");
			
			fileStream = new DataInputStream(new FileInputStream(destinationPath+"\\chart.pdf"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}
	
	

	public InputStream getFileStream() {
		return fileStream;
	}

	public void setFileStream(InputStream fileStream) {
		this.fileStream = fileStream;
	}

	public int getIsListEmpty() {
		return isListEmpty;
	}

	public void setIsListEmpty(int isListEmpty) {
		this.isListEmpty = isListEmpty;
	}

	public List<SoldProducts> getSoldProductsList() {
		return soldProductsList;
	}

	public void setSoldProductsList(List<SoldProducts> soldProductsList) {
		this.soldProductsList = soldProductsList;
	}

	public int getTopanswer() {
		return topanswer;
	}

	public void setTopanswer(int topanswer) {
		this.topanswer = topanswer;
	}

}
